﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Admins
{
    public partial class Pages : System.Web.UI.Page
    {
        private string Lang = "";
        static string Level = "";
        SqlDataProvider sql = new SqlDataProvider();
        protected void Page_Load(object sender, EventArgs e)
        {
            Lang = Session["Lang"].ToString();
            if (!IsPostBack)
            {
                lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
                lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
                NumberClass.OnlyInputNumber(txtOrd);
                BindGrid();
            }
        }

        private void BindGrid()
        {
            grdPages.DataSource = PagesService.Pages_GetByAll(Lang);
            grdPages.DataBind();
            if (grdPages.PageCount <= 1)
            {
                grdPages.PagerStyle.Visible = false;
            }
        }
        private void LoadDropDownListPageLinkType()
        {
            string[] myArr = new string[] { "1,Nhập liên kết", "2,Liên kết module" };
            Common.PageHelper.LoadDropDownList(ddlLinkType, myArr, true);
        }
        protected string ShowType(string type)
        {
            return type.Equals("1") ? "Trang liên kết" : "Trang nội dung";
        }
        protected string ShowNameLevel(string Name, string Level)
        {
            int strLevel = (Level.Length / 5);
            string strReturn = "";
            for (int i = 1; i < strLevel; i++)
            {
                strReturn = strReturn + ".....";
            }
            strReturn += Name;
            return strReturn;
        }
        protected void grdPages_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdPages.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex.ToString() + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex.ToString() + ")");
                    }
                }
            }
        }
        protected string possition(string posi)
        {
            if(posi.Equals("1"))
            {
                return "menu trên";
            }
            if (posi.Equals("2"))
            {
                return "menu trái";
            }
            else
            {
                return "menu dưới";
            }
        }
        protected void grdPages_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdPages.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grdPages_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "AddSub":
                    hfLevel.Value = strCA;
                    AddButton_Click(source, e);
                    break;
                case "Edit":
                    Session["Insert"] = "false";
                    hfId.Value = strCA;
                    List<Data.Pages> listE = PagesService.Pages_GetById(hfId.Value);
                    Level = listE[0].Level.Substring(0, listE[0].Level.Length - 5);
                    txtName.Text = listE[0].Name;
                    //txtImage.Text = listE[0].Images;
                    fckDetail.Value = listE[0].Detail;
                    txtTitle.Text = listE[0].Title;
                    txtDescription.Text = listE[0].Description;
                    txtKeyword.Text = listE[0].Keyword;
                    LoadDropDownListPageLinkType();
                    LoadDropDownListPageLink();
                    PageHelper.LoadDropDownListPagePosition(ddlPosition);
                    PageHelper.LoadDropDownListPageType(ddlType);
                    PageHelper.LoadDropDownListTarget(ddlTarget);
                    ddlType.SelectedValue = listE[0].Type;
                    txtLink.Text = listE[0].Link;
                    ddlTarget.SelectedValue = listE[0].Target;
                    ddlPosition.SelectedValue = listE[0].Position;
                    try
                    {
                        ddlLink.Text = listE[0].Link;
                        ddlLinkType.Text = "2";
                    }
                    catch
                    {
                        ddlLinkType.Text = "1";
                    }
                    txtOrd.Text = listE[0].Ord;
                    chkActive.Checked = listE[0].Active == "1" || listE[0].Active == "True";
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;
                case "Active":
                    string strA = "";
                    string str = e.Item.Cells[2].Text;
                    strA = str == "1" ? "0" : "1";
                    SqlDataProvider sql = new SqlDataProvider();
                    sql.ExecuteNonQuery("Update [Page] set Active=" + strA + "  Where Id='" + strCA + "'");
                    BindGrid();
                    break;
                case "Delete":
                    PagesService.Pages_Delete(strCA);
                    BindGrid();
                    break;
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            LoadDropDownListPageLinkType();
            LoadDropDownListPageLink();
            PageHelper.LoadDropDownListPagePosition(ddlPosition);
            PageHelper.LoadDropDownListPageType(ddlType);
            PageHelper.LoadDropDownListTarget(ddlTarget);
            pnView.Visible = false;
            Session["Insert"] = "true";
        }
        private void LoadDropDownListPageLink()
        {
            ddlLink.Items.Clear();
            ddlLink.Items.Add(new ListItem("---Chọn liên kết---", ""));
            ddlLink.Items.Add(new ListItem("Trang chủ", "/"));
            ddlLink.Items.Add(new ListItem("Liên hệ", "/lien-he.htm"));
            ddlLink.Items.Add(new ListItem("-----------------------------", ""));
            var list = GroupNewsService.GroupNews_GetByAll(Lang);
            if (list.Count > 0)
            {
                foreach (var t in list)
                {
                    ddlLink.Items.Add(new ListItem(StringClass.ShowNameLevel(t.Name, t.Level), "/tin-tuc/" + t.Tag + "/" + t.Id + ".htm"));
                }
            }
            ddlLink.Items.Add(new ListItem("-----------------------------", ""));
            var listser = GroupServiceService.GroupService_GetByAll(Lang);
            foreach (var t in listser)
            {
                ddlLink.Items.Add(new ListItem(StringClass.ShowNameLevel(t.Name, t.Level), "/san-pham/" + t.Tag + "/" + t.Id + ".htm"));
            }
            ddlLink.DataBind();
            list.Clear();
        }
        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            DataGridItem item;
            for (int i = 0; i < grdPages.Items.Count; i++)
            {
                item = grdPages.Items[i];
                if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                {
                    if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                    {
                        string strId = item.Cells[1].Text;
                        PagesService.Pages_Delete(strId);
                    }
                }
            }
            grdPages.CurrentPageIndex = 0;
            BindGrid();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BindGrid();
        }
        protected void Update_Click(object sender, EventArgs e)
        {
            string Links = "";
            if (Page.IsValid)
            {
                var obj = new Data.Pages();
                obj.Id = hfId.Value;
                obj.Name = txtName.Text;
                //obj.Image = txtImage.Text;
                obj.Tag = StringClass.NameToTag(txtName.Text);
                obj.Detail = fckDetail.Value;
                obj.Level = Level + "00000";
                obj.Title = txtTitle.Text;
                obj.Description = txtDescription.Text;
                obj.Keyword = txtKeyword.Text;
                obj.Type = ddlType.SelectedValue;
                obj.Link = txtLink.Text;
                obj.Target = ddlTarget.SelectedValue;
                obj.Position = ddlPosition.SelectedValue;
                obj.Ord = txtOrd.Text != "" ? txtOrd.Text : "1";
                obj.Active = chkActive.Checked ? "1" : "0";
                obj.Lang = Lang;
                if (Session["Insert"].Equals("true"))
                {
                    PagesService.Pages_Insert(obj);
                    hfId.Value = sql.MaxId("Pages", "Id");
                }
                else
                {
                    PagesService.Pages_Update(obj);
                }
                if (ddlType.SelectedValue == "2")
                {
                    Links = "/"+ StringClass.NameToTag(txtName.Text) + "/" + hfId.Value + ".htm";
                    sql.ExecuteNonQuery("Update Pages set Link='" + Links + "' Where Id='" + hfId.Value + "'");
                }
                BindGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                hfLevel.Value = "";
                Session["Insert"] = "false";
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            hfLevel.Value = "";
            Session["Insert"] = "false";
        }
    }
}